﻿using AntdUI;

namespace DataAcquisition
{
    public partial class ManageTemplateForm : BaseForm
    {
        #region 属性
        private Template obj = new Template();
        #endregion

        #region 自定义方法
        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadList()
        {
            List<Template> list = SqlHelper.Inquireable<Template>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Template> list)
        {
            var antItemList = new List<TemplateItem>();
            foreach (Template item in list)
            {
                string first = string.Empty;
                if (item.FirstStagePressure > 0 && item.FirstStageTime > 0)
                {
                    string timeUnit = item.FirstStageTimeUnit;
                    first = string.Format("压力:{0}Mpa--时长：{1}{2}", item.FirstStagePressure.ToString(), item.FirstStageTime.ToString(), timeUnit);
                }

                string second = string.Empty;
                if (item.SecondStagePressure > 0 && item.SecondStageTime > 0)
                {
                    string timeUnit = item.SecondStageTimeUnit;
                    second = string.Format("压力:{0}Mpa--时长：{1}{2}", item.SecondStagePressure.ToString(), item.SecondStageTime.ToString(), timeUnit);
                }

                string third = string.Empty;
                if (item.ThirdStagePressure > 0 && item.ThirdStageTime > 0)
                {
                    string timeUnit = item.ThirdStageTimeUnit;
                    third = string.Format("压力:{0}Mpa--时长：{1}{2}", item.ThirdStagePressure.ToString(), item.ThirdStageTime.ToString(), timeUnit);
                }

                string intensity = string.Empty;
                if (item.IntensityStagePressure > 0 && item.IntensityStageTime > 0)
                {
                    string timeUnit = item.IntensityStageTimeUnit;
                    intensity = string.Format("压力:{0}Mpa--时长：{1}{2}", item.IntensityStagePressure.ToString(), item.IntensityStageTime.ToString(), timeUnit);
                }

                string rigor = string.Empty;
                if (item.RigorStagePressure > 0 && item.RigorStageTime > 0)
                {
                    string timeUnit = item.RigorStageTimeUnit;
                    rigor = string.Format("压力:{0}Mpa--时长：{1}h", item.RigorStagePressure.ToString(), item.RigorStageTime.ToString(), timeUnit);
                }

                string pipe_position = string.Empty;
                if (item.PipePosition is not null)
                {
                    pipe_position = item.PipePosition;
                }

                antItemList.Add(new TemplateItem(item.ID,item.Number, pipe_position, item.DesignPressure,
                    first, second, third, intensity, rigor));
            }
            Table1.DataSource = antItemList;
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckRowExist(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Template> list = SqlHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                obj = list[0];
                return true;
            }
            else
            {
                string msg = string.Format("【{0}】模板不存在", number);
                Error(msg);
                obj = new Template();
                return false;
            }
        }
        #endregion

        #region 系统方法
        public ManageTemplateForm()
        {
            InitializeComponent();
            FullScreen = true;
            Header.Text = "管理试压模板";
        }

        private void ManageTemplateForm_Load(object sender, EventArgs e)
        {
            
            Table1.Height = this.Height;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("Number","模板编号",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("PipePosition","管道位置",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("DesignPressure","设计压力",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("FirstStage","第一阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("SecondStage","第二阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("ThirdStage","第三阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("IntensityStage","强度阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("RigorStage","严密阶段",AntdUI.ColumnAlign.Center),
                new AntdUI.Column("Operation","操作",AntdUI.ColumnAlign.Center)
            };
            Table1.Spin("正在加载中...", (config) =>
            {
                LoadList();
            });
        }

        private void Table1_CellButtonClick(object sender, TableButtonEventArgs e)
        {
            if (e.Record is TemplateItem data)
            {
                CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                }
                else if (link.Id == "delete")
                {
                    bool res = CheckRowExist(data.Number);
                    if (!res)
                    {
                        return;
                    }

                    string msg = string.Format("您确定要删除【{0}】模板吗？", data.Number);
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", msg)
                    {
                        OnOk = config =>
                        {
                            if (obj is not null)
                            {
                                List<Template> list = new List<Template>();
                                list.Add(obj);
                                int deleteRes = SqlHelper.Deleteable(list);
                                if (deleteRes > 0)
                                {
                                    LoadList();
                                }
                                else
                                {
                                    Error("删除模板失败");
                                }
                            }
                            else
                            {
                                Error("删除模板失败");
                            }
                            return true;
                        },
                    });
                }
            }
        }
        #endregion
    }
}
